package ma.xin.shi.arxml.export.cherySomeip.matrix;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import ma.xin.shi.ConfigurationManager;
import ma.xin.shi.configurations.dataType.DataTypeEntry;


public class CherySomeipArxmlExportData {

    private final List<DataTypeEntry> allDataTypes;

    public CherySomeipArxmlExportData(ConfigurationManager configManager) {
        this.allDataTypes = configManager.getDataTypeConfig().getAllDataTypes();
    }

    public void export() {
        StringBuilder xmlBuilder = new StringBuilder();

        // 开始构建 XML
        xmlBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        xmlBuilder.append("<DATA-TYPES>\n");

        for (DataTypeEntry entry : allDataTypes) {
            appendDataType(xmlBuilder, entry);
        }

        xmlBuilder.append("</DATA-TYPES>\n");

        // 输出 XML 到控制台
        System.out.println(xmlBuilder.toString());

        // 可选：保存到文件
        writeToFile(xmlBuilder.toString(), "D:\\Chery_DataTypes.arxml");
    }

    private void appendDataType(StringBuilder sb, DataTypeEntry entry) {
        sb.append("  <DATA-TYPE-DEFINITION>\n");
        sb.append("    <SHORT-NAME>").append(entry.getDataTypeName()).append("</SHORT-NAME>\n");
        sb.append("    <CATEGORY>").append(entry.getDataType()).append("</CATEGORY>\n");

        if ("Base".equals(entry.getDataType())) {
            appendBaseType(sb, entry);
        } else if ("Struct".equals(entry.getDataType())) {
            appendStructType(sb, entry);
        } else if ("Array".equals(entry.getDataType())) {
            appendArrayType(sb, entry);
        } else if ("Enum".equals(entry.getDataType())) {
            appendEnumType(sb, entry);
        }

        sb.append("  </DATA-TYPE-DEFINITION>\n");
    }

    private void appendBaseType(StringBuilder sb, DataTypeEntry entry) {
        sb.append("    <SW-DATA-DEF-PROPS>\n");
        sb.append("      <SW-DATA-DEF-PROP-VARIANTS>\n");
        sb.append("        <SW-DATA-DEF-PROP>\n");
        sb.append("          <BASE-TYPE-REF>/AUTOSAR_Platform/ImplementationDataTypes/");
        sb.append(entry.getBaseType()).append("</BASE-TYPE-REF>\n");
        sb.append("        </SW-DATA-DEF-PROP>\n");
        sb.append("      </SW-DATA-DEF-PROP-VARIANTS>\n");
        sb.append("    </SW-DATA-DEF-PROPS>\n");
    }

    private void appendStructType(StringBuilder sb, DataTypeEntry entry) {
        sb.append("    <STRUCTURE-MEMBERS>\n");
        for (String[] member : entry.getStructMembers()) {
            sb.append("      <MEMBER>\n");
            sb.append("        <SHORT-NAME>").append(member[0]).append("</SHORT-NAME>\n");
            sb.append("        <TYPE-REF>/DataTypes/").append(member[1]).append("</TYPE-REF>\n");
            sb.append("      </MEMBER>\n");
        }
        sb.append("    </STRUCTURE-MEMBERS>\n");
    }

    private void appendArrayType(StringBuilder sb, DataTypeEntry entry) {
        sb.append("    <ARRAY-PROPS>\n");
        sb.append("      <LENGTH-TYPE>").append(entry.getArrayLengthType()).append("</LENGTH-TYPE>\n");
        sb.append("      <MIN-LENGTH>").append(entry.getArrayLengthMin()).append("</MIN-LENGTH>\n");
        sb.append("      <MAX-LENGTH>").append(entry.getArrayLengthMax()).append("</MAX-LENGTH>\n");
        sb.append("      <ELEMENT-TYPE>").append(entry.getArrayMemberType()).append("</ELEMENT-TYPE>\n");
        sb.append("    </ARRAY-PROPS>\n");
    }

    private void appendEnumType(StringBuilder sb, DataTypeEntry entry) {
        sb.append("    <ENUM-PROPS>\n");
        for (String[] member : entry.getEnumMembers()) {
            sb.append("      <ENUM-MEMBER>\n");
            sb.append("        <SHORT-NAME>").append(member[0]).append("</SHORT-NAME>\n");
            sb.append("        <VALUE>").append(member[1]).append("</VALUE>\n");
            sb.append("      </ENUM-MEMBER>\n");
        }
        sb.append("    </ENUM-PROPS>\n");
    }

    private void writeToFile(String content, String filePath) {
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write(content);
            System.out.println("文件已成功写入: " + filePath);
        } catch (IOException e) {
            System.err.println("写入文件失败: " + filePath);
            e.printStackTrace();
        }
    }
}
